package kr.couponplay.android.activity.info;

import java.util.Date;
import java.util.List;

import kr.couponplay.android.AppContext;
import kr.couponplay.android.activity.R;
import kr.couponplay.android.domain.deal.Provider;
import kr.couponplay.android.support.DateUtils;
import kr.couponplay.android.support.DisplayUtils;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;

@SuppressLint("HandlerLeak")
public class InfoActivity extends PreferenceActivity implements OnPreferenceClickListener {
	InfoActivity activity = this;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.layout.setting_activity);
        
        Preference curVersion = (Preference)findPreference(getResources().getString(R.string.info_cur_version_key));
        curVersion.setSummary(getVersionName());
        curVersion.setOnPreferenceClickListener(this);

        Preference newVersion = (Preference)findPreference(getResources().getString(R.string.info_new_version_key));
        newVersion.setSummary(AppContext.getNewAppVersion(getApplicationContext()));
        newVersion.setOnPreferenceClickListener(this);
        
        Preference maker = (Preference)findPreference(getResources().getString(R.string.info_maker_key));
        maker.setSummary(AppContext.makerName + " " + AppContext.makerEmail);
        maker.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			@Override
			public boolean onPreferenceClick(Preference preference) {
				Uri uri = Uri.parse("mailto:" + AppContext.makerEmail);
				Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
				startActivity(intent);
				return false;
			}
        });
        
        Preference providers = (Preference)findPreference(getResources().getString(R.string.info_provider_key));
        providers.setSummary(getProviders());
        
        Preference dealCount = (Preference)findPreference(getResources().getString(R.string.info_deal_count_key));
        dealCount.setSummary(DisplayUtils.getNumberFormat(AppContext.getDealTotalCount(getApplicationContext())));
        
        Preference updateDate = (Preference)findPreference(getResources().getString(R.string.info_deal_update_key));
        updateDate.setSummary(DateUtils.toString(new Date(AppContext.getLastUpdatedTime(getApplicationContext()))));
	}
	
	private CharSequence getProviders() {
		List<Provider> providers = AppContext.getProviders(getApplicationContext());
		if (providers == null) {
			return "";
		}
		
		String providerStr = "";
		for (Provider provider : providers) {
			providerStr = providerStr + " " + provider.getName();
		}
		return providerStr;
	}

	private String getVersionName() {
		try {
		    PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
		    return pInfo.versionName;
		} catch (NameNotFoundException e) {
		    return "";
		}
	}

	@Override
	public boolean onPreferenceClick(Preference preference) {
		if (!getVersionName().equals(AppContext.getAdPlatformType(getApplicationContext()))) {
			startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://tsto.re/0000319472")));
		}
		return false;
	}
}
